//
//  KLCertificationViewController.m
//  KISSLOCK
//
//  Created by 华杨科技 on 2017/4/20.
//  Copyright © 2017年 陈宾鹏. All rights reserved.
//
// 上传身份证照片

#import "KLCertificationViewController.h"
#import "HemaConst.h"
#import "KLHelp.h"
#import "SKFCamera.h"
#import "UIImageView+WebCache.h"

@interface KLCertificationViewController () <UIImagePickerControllerDelegate,UINavigationControllerDelegate>
{
    
}

@property (weak, nonatomic) IBOutlet UIImageView *pictureIV; // 身份证图片
@property (weak, nonatomic) IBOutlet UILabel *descriptionLB; // 说明文字
@property (weak, nonatomic) IBOutlet UIButton *addPictureBtn;

@property (nonatomic, strong) UIImage *image; // 保存从相机/相册中选取的图片

@end

@implementation KLCertificationViewController

- (void)viewDidLoad {
    [super viewDidLoad];

    // 判断情况
    if (self.pictureType == IdentifyCardPictureFront) { // 正面
        [self navSetFront];
    }else { // 反面
        [self navSetHoldInHand];
    }
}


- (void)navSetFront {
    [KLHelp navSet:self doBackHandler:^(UIButton *backButton) {
        [backButton addTarget:self
                       action:@selector(doBack:) forControlEvents:(UIControlEventTouchUpInside)];
        self.descriptionLB.text = @"请拍摄并上传您完整清晰的身份证照片，便于我们确认真实姓名和头像两项和您本人相符。";
        
        UIImage *placeImage = [UIImage imageNamed:@"身份证正面照上传.png"];
        [self.pictureIV sd_setImageWithURL:nil placeholderImage:placeImage];
        
    } nextStepHandler:^(UIButton *nextStepBtn) {
        [nextStepBtn addTarget:self action:@selector(nextStep:) forControlEvents:(UIControlEventTouchUpInside)];
    }];
}

- (void)navSetHoldInHand {
    [KLHelp navSet:self doBackHandler:^(UIButton *backButton) {
        [backButton addTarget:self
                       action:@selector(doBack:) forControlEvents:(UIControlEventTouchUpInside)];
    } nextStepHandler:^(UIButton *nextStepBtn) {
        [nextStepBtn setTitle:@"完成" forState:(UIControlStateNormal)];
        // 设置图片
        UIImage *placeImage = [UIImage imageNamed:@"手持身份证上传.png"];
        [self.pictureIV sd_setImageWithURL:nil placeholderImage:placeImage];
//        self.pictureIV.image =
        // 说明文字
        self.descriptionLB.text = @"请再上传一张您手持身份证的完整清晰照片，将根据照片确认当前是您本人正在操作。";

        [nextStepBtn addTarget:self action:@selector(nextStep:) forControlEvents:(UIControlEventTouchUpInside)];
    }];
}
- (void)doBack:(UIButton *)sender {
    [self.navigationController popViewControllerAnimated:YES];
}

- (void)nextStep:(UIButton *)sender {

    if (self.pictureType == IdentifyCardPictureFront) {
        // 本页跳转 // 重新给状态赋值, 否则跳不出去
        self.pictureType = IdentifyCardPictureFront;
        
        KLCertificationViewController *certifyVC = [[KLCertificationViewController alloc] initWithNibName:@"KLCertificationViewController" bundle:nil];
        certifyVC.title = @"实名认证";
        [self.navigationController pushViewController:certifyVC animated:YES];
    }else {

        [self.navigationController popToRootViewControllerAnimated:YES];
    }
}


#pragma mark 添加图片
- (IBAction)addPicAction:(UIButton *)sender {
    
    UIAlertController *alertC = [[UIAlertController alloc] init];
    UIAlertAction *takePhotoAction = [UIAlertAction actionWithTitle:@"拍照" style:(UIAlertActionStyleDefault) handler:^(UIAlertAction * _Nonnull action) {
       // 判断相机权限是否打开
        if ([UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypeCamera]) {
            SKFCamera *cameraVC = [[SKFCamera alloc] init];
            __weak typeof(self) weakSelf  = self;
            cameraVC.fininshcapture = ^(UIImage *image) {
                if (image) {
                    NSLog(@"照片存在");
                    // 上传图片
                    weakSelf.pictureIV.image = image;
                }
            };
            [weakSelf presentViewController:cameraVC animated:YES completion:nil];
        }else {
            NSLog(@"调用相机失败");
        }
    }];
    
    UIAlertAction *albumAction = [UIAlertAction actionWithTitle:@"相册" style:(UIAlertActionStyleDefault) handler:^(UIAlertAction * _Nonnull action) {
        UIImagePickerController *pickerVC = [[UIImagePickerController alloc] init];
        pickerVC.allowsEditing = YES;
        pickerVC.delegate = self;
        pickerVC.sourceType =  UIImagePickerControllerSourceTypeSavedPhotosAlbum;
        pickerVC.mediaTypes = [UIImagePickerController availableMediaTypesForSourceType:UIImagePickerControllerSourceTypeSavedPhotosAlbum];
        [self presentViewController:pickerVC animated:YES completion:nil];
    }];
    
    UIAlertAction *cancelAction = [UIAlertAction actionWithTitle:@"取消" style:(UIAlertActionStyleCancel) handler:nil];
    
    [alertC addAction:takePhotoAction];
    [alertC addAction:albumAction];
    [alertC addAction:cancelAction];
    
    [self presentViewController:alertC animated:YES completion:nil];
}

#pragma mark - UIImagePickerControllerDelegate
- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary<NSString *,id> *)info {

    UIImage *image = [info objectForKey:@"UIImagePickerControllerOriginalImage"];
    self.image = image;
    self.pictureIV.image = self.image;
    [self dismissViewControllerAnimated:YES completion:nil];
}

- (void)imagePickerControllerDidCancel:(UIImagePickerController *)picker {
    [self dismissViewControllerAnimated:YES completion:nil];
}



/**上传照片*/
// 1 表示上传身份证正面，2 反面
- (void)submitImage:(NSString *)type{
  
}

@end
